use std::cmp::Ordering;
use rand::Rng;
use std::io;

fn main() {
    let mut rng = rand::thread_rng();
    let random_num = rng.gen_range(1..101);
    println!("{}", random_num);
    loop {
        println!("Guess the number!");
        let mut guess = String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please type a number!");
                continue;
            }
        };
        println!("You guessed: {}", guess);
        // if写法
        // if random_num == guess {
        //     println!("You win!");
        //     break;
        // } else if guess < random_num {
        //     println!("Too small!");
        // } else {
        //     println!("Too big!");
        // }

        // math 写法
        match guess.cmp(&random_num) {
            Ordering::Less => { println!("Too small!"); }
            Ordering::Equal => {
                println!("You win!");
                break;
            }
            Ordering::Greater => { println!("Too big!"); }
        }
    }
}